﻿using StarPizza.Model.BaseModel;
using StarPizza.Model.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.ViewModel.Core
{
    public class PizzaVM : IPizza, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private IPizzaTemplate pizzaTemplate;
        public IPizzaTemplate PizzaTemplate
        {
            get { return pizzaTemplate; }
            set
            {
                if (value != pizzaTemplate)
                {
                    pizzaTemplate = value;
                    OnPropertyChanged("ToppingsDescription");
                    IngredientsChanged();
                }
            }
        }

        private void IngredientsChanged()
        {
            OnPropertyChanged("Toppings");
            OnPropertyChanged("Cost");
            OnPropertyChanged("Price");
            OnPropertyChanged("Name");
        }
        private Dough dough;
        public Dough Dough
        {
            get { return dough; }
            set
            {
                if (value != dough)
                {
                    dough = value;
                    IngredientsChanged();
                }
            }
        }

        #region Evaluator properties:
        public IEnumerable<Topping> Toppings
        {
            get 
            { 
                if (PizzaTemplate != null)
                    return this.PizzaTemplate.Toppings;
                else
                    return new List<Topping>();
            }
        }
        public int Cost
        {
            get
            {
                int doughCost = 0;
                if (Dough != null)
                    doughCost = Dough.Cost;
                return doughCost + Toppings.Sum(topping => topping.Cost);
            }
        }
        public int Price
        {
            get
            {
                int doughPrice = 0;
                if (Dough != null)
                    doughPrice = Dough.Price;
                return doughPrice + Toppings.Sum(topping => topping.Price);
            }
        }
        public int Size
        {
            get 
            {
                if (Dough != null)
                    return Dough.Size;
                else
                    return 0;
            }
        }
        public string ToppingsDescription
        {
            get
            {
                return PizzaTemplateVM.GetToppingsDescription(pizzaTemplate);
            }
        }
        public string Name
        {
            get
            {
                string templateName = "";
                if (pizzaTemplate != null)
                    templateName = pizzaTemplate.Name;
                return templateName + " (" + Size + " cm)";
            }
        }
        #endregion evaluator properties
    }
}
